﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Core.IServices;
using DI;
using Core.Dtos;
using Core.Queries;
using Core.Messages;
using Microsoft.AspNetCore.Cors;

namespace API.Controllers
{
    [Route("api/[controller]s")]
    [EnableCors("AllowAll")]
    public class PersonController : Controller
    {
        private IPersonService personService;

        public PersonController()
        {
            this.personService = Bootstrapper.Resolve<IPersonService>();
        }

        [HttpGet]
        public QueryResponse<PersonDto> Get(PersonQuery query)
        {
            return personService.FindBy(query);
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public Response<PersonDto> Get(int id)
        {
            return personService.FindById(id);
        }        

        // POST api/values
        [HttpPost]        
        public Response<int> Post([FromBody]PersonDto dto)
        {
            return personService.Add(dto);
        }

        // PUT api/values/5
        [HttpPut("{id}")]        
        public Response Put(int id, [FromBody]PersonDto dto)
        {
            return personService.Edit(id, dto);
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public Response Delete(int id)
        {
            return personService.Delete(id);
        }
    }
}
